package frontend.components.base;

import backend.model.base.DBModel;
import frontend.components.other.DBModelChoiceRenderer;
import org.apache.wicket.Component;
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.util.CollectionModel;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.ResourceReference;

import java.util.Iterator;
import java.util.List;

public class PaletteComponent<ModelType, EntityType extends DBModel> extends Panel {

    private Palette<EntityType> palette;

    public PaletteComponent(final String propertyId, ModelType uiModel, List<EntityType> availableItems,
                            int itemsNoToShow, boolean allowOrdering) {
        this(propertyId, uiModel, new CollectionModel<EntityType>(availableItems), itemsNoToShow, allowOrdering);
    }

    public PaletteComponent(final String propertyId, ModelType uiModel, CollectionModel<EntityType> availableItems,
                            int itemsNoToShow, boolean allowOrdering) {
        super(propertyId);

        palette = new Palette<EntityType>("palette", new PropertyModel<List<? extends EntityType>>(uiModel, propertyId),
                availableItems, new DBModelChoiceRenderer<EntityType>(), itemsNoToShow, allowOrdering) {

            protected Component newSelectedHeader(String componentId) {
                return new Label(componentId, new ResourceModel("palette." + propertyId + ".selected", "Selected"));
            }

            protected Component newAvailableHeader(String componentId) {
                return new Label(componentId, new ResourceModel("palette." + propertyId + ".available", "Available"));
            }

            protected ResourceReference getCSS() {
                return new PackageResourceReference(PaletteComponent.class, "hack-css-that-does-not-exist.css");
            }

        };

        add(palette);
        setOutputMarkupId(true);
    }

    public Iterator<EntityType> getUnselectedChoices() {
        Iterator<EntityType> unselectedChoices = palette.getRecorderComponent().getUnselectedChoices();
        return unselectedChoices;
    }
}
